/**
 * 
 */
package optimization.naturalOptimization.terminationCriterion;

import optimization.naturalOptimization.NaturalOptimization;

/**
 * This class provides a termination criterion for termination, if change of
 * best, worst, mean and standard deviation is lass than a given percentage.
 * 
 * @author Kevin Wagner
 * @version 1.0
 */
public class NoChangeTerminationCriterion extends TerminationCriterion {

	/**
	 * Creates a new Termination Criterion with a given parent optimization.
	 * 
	 * @param opt
	 *            parent optimization
	 */
	public NoChangeTerminationCriterion(NaturalOptimization opt) {
		super(opt);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see optimization.IdentificationI#getIndentification()
	 */
	@Override
	public String getIndentification() {
		return "No Change Termination Criterion";
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * optimization.naturalOptimization.terminationCriterion.TerminationCriterion
	 * #isFinished()
	 */
	@Override
	public boolean isFinished() {
		int n = getParentOptimization().getChangeInterval();
		if (getParentOptimization().getCurrentGeneration() < n) {
			return false;
		}
		double current = getParentOptimization().getResults().get(
				getParentOptimization().getCurrentGeneration())[3];
		double last = getParentOptimization().getResults().get(
				getParentOptimization().getCurrentGeneration() - n)[3];
		double percent = getParentOptimization().getChangePercentage();
		if (Math.abs((current / last - 1.0)) > percent) {
			return false;
		}
		return true;
	}

}
